package com.bbm.communitysystem.controller.role;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.bbm.communitysystem.core.domain.R;
import com.bbm.communitysystem.domain.Role;
import com.bbm.communitysystem.domain.UserInfo;
import com.bbm.communitysystem.domain.dto.role.UserRoleDto;
import com.bbm.communitysystem.domain.vo.PageVo;
import com.bbm.communitysystem.domain.vo.UserRoleVo;
import com.bbm.communitysystem.service.IRoleService;
import com.bbm.communitysystem.service.IUserInfoService;
import com.bbm.communitysystem.tools.DemoDate;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

@Tag(name = "用户角色")
@RestController
@RequestMapping("/userRole")
@RequiredArgsConstructor
public class UserRoleController {

    private final IUserInfoService userInfoService;

    private final IRoleService roleService;

    @Operation(summary = "修改用户角色")
    @PutMapping()
    public R<String> putUserRole(@RequestBody UserRoleDto userRoleDto) {

        boolean update = userInfoService.lambdaUpdate()
                .eq(UserInfo::getId, userRoleDto.getUserId())
                .set(UserInfo::getUserRoleId, userRoleDto.getRoleId())
                .update();

        return update ? R.ok("修改成功") : R.error("修改失败");
    }


    @Operation(summary = "获取用户角色")
    @GetMapping()
    public R<PageVo<UserRoleVo>> getUserRole(
            @RequestParam(defaultValue = "1") Integer current,
            @RequestParam(defaultValue = "10") Integer size) {

        Page<UserInfo> page = userInfoService.page(new Page<>(current, size));

        List<UserRoleVo> userRoleVos = page.getRecords().stream().map(user -> {
            UserRoleVo vo = new UserRoleVo();
            vo.setUserId(user.getId());
            vo.setUserNick(user.getUserNick()); // 假设UserInfo有nick字段
            vo.setStatus(user.getStatus());

            String userRoleId = user.getUserRoleId();


            vo.setRoleId(Arrays.stream(userRoleId.split(",")).toList());

            List<String> roleNames = new ArrayList<>();

            String[] split = userRoleId.split(",");

            for (String s : split) {
                if ("0".equals(s)) {
                    roleNames.add("用户");
                } else {
                    Role role = roleService.getById(s);
                    roleNames.add(role.getRoleName());
                }

            }

            vo.setTime(DemoDate.getPostDate(user.getUpdateTime()));
            vo.setRoleName(roleNames);

            return vo;
        }).collect(Collectors.toList());

        PageVo<UserRoleVo> pageVo = new PageVo<>();
        pageVo.setRecords(userRoleVos);
        pageVo.setTotal(page.getTotal());
        pageVo.setCurrent(page.getCurrent());
        pageVo.setSize(page.getSize());

        return R.ok(pageVo);
    }

}
